package com.ciaojian.store.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ciaojian.core.enums.business.ScheduleTypeEnum;
import com.ciaojian.core.enums.business.WorkerApplyStatusEnum;
import com.ciaojian.core.mapper.EatTimeMapper;
import com.ciaojian.core.mapper.SchedulingExamineMapper;
import com.ciaojian.core.mapper.SchedulingMapper;
import com.ciaojian.core.mapper.WorkerMapper;
import com.ciaojian.core.model.EatTime;
import com.ciaojian.core.model.Scheduling;
import com.ciaojian.core.model.SchedulingExamine;
import com.ciaojian.core.model.Worker;
import com.ciaojian.core.model.dto.RosterDTO;
import com.ciaojian.core.model.dto.SchedulingDTO;
import com.ciaojian.core.service.SchedulingExamineService;
import com.ciaojian.core.util.TimeUtil;
import com.ciaojian.store.model.query.ScheduleExamineQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Atlakyn
 */
@Service
public class SchedulingExamineServiceImpl extends SchedulingExamineService<SchedulingExamineMapper, SchedulingExamine> implements IService<SchedulingExamine> {
    @Resource
    private SchedulingServiceImpl schedulingService;
    @Resource
    private SchedulingMapper schedulingMapper;
    @Resource
    private WorkerMapper workerMapper;
    @Resource
    private EatTimeMapper eatTimeMapper;


    /**
     * 审核排班申请
     *
     * @param schedulingExamine 排班审核
     * @param query             查询条件
     * @param examineId         排班申请Id
     */
    public void audit(SchedulingExamine schedulingExamine,
                      ScheduleExamineQuery query,
                      Integer examineId) {
        // 更新审核信息
        schedulingExamine.setOperator(query.getOperator());
        schedulingExamine.setStatus(query.getStatus());

        List<Scheduling> schedulingList = new ArrayList<>();

        // 若是驳回则填充驳回理由
        if (WorkerApplyStatusEnum.DISMISSED.equals(query.getStatus())) {
            schedulingExamine.setDisallowReason(query.getDisallowReason());

            QueryWrapper<Scheduling> qw = new QueryWrapper<>();
            qw.eq("examine_id", examineId);

            List<Scheduling> schedulings = schedulingMapper.selectList(qw);

            for (Scheduling scheduling : schedulings) {
                scheduling.setStatus(query.getStatus());
                schedulingList.add(scheduling);
            }

            schedulingService.updateBatchById(schedulingList);
        }else if (WorkerApplyStatusEnum.PASS.equals(query.getStatus())) {
            // 若是通过,修改全部排班记录为通过
            List<SchedulingDTO> list = schedulingService.getBaseMapper().findByExamineId(examineId);
            // 用来判断是否为同一天
            Date date = null;
            // 用来轮询哪个时间段, true为上半场 false为下半场
            boolean turn = true;
            /*
             * 在排班时需要为发型师错开就餐时间以避免某段时间内门店完全没有人值班,例如:
             * 中午就餐时间段为 12:00-13:00
             * 发型师A需要在 12:00-12:30 段内就餐, 发型师B需要在 12:30-13:00 段内就餐
             */
            for (SchedulingDTO schedulingDTO : list) {
                if (schedulingDTO.getDroster().equals("请假")&&schedulingDTO.getType()== ScheduleTypeEnum.LEAVE){
                    continue;
                }
                // 如果排班日期和 date 变量不同, 表示已经不是同一天的排班
                // 则重置排班日期和 turn 变量
                if (!schedulingDTO.getDate().equals(date)) {
                    turn = true;
                    date = schedulingDTO.getDate();
                }
                RosterDTO roster = schedulingDTO.getRoster();
                if (roster.getId()==-1){
                    roster = null;
                }

                Scheduling scheduling = new Scheduling().selectById(schedulingDTO.getId());
                Worker worker = workerMapper.selectById(scheduling.getWorkerId());
                if (ObjectUtil.isNull(worker.getEatTimeId()) || worker.getEatTimeId() == 0) {
//                    if (turn && roster != null) {
//                        switch (roster.getEatingType()) {
//                            case 0:
//                                // 轻餐开始时间=班次设定开始时间
//                                scheduling.setLunchStartTime(roster.getLunchStartTime());
//                                // 轻餐结束时间=开始时间15分钟后
//                                scheduling.setLunchEndTime(TimeUtil.offset(roster.getLunchStartTime(), roster.getLightTime()));
//                                break;
//                            case 1:
//                                // 就餐开始时间=班次设定开始时间
//                                scheduling.setLunchStartTime(roster.getLunchStartTime());
//                                // 就餐结束时间=开始时间30分钟后
//                                scheduling.setLunchEndTime(TimeUtil.offset(roster.getLunchStartTime(), roster.getMainTime()));
//                                break;
//                            case 2:
//                                break;
//                            default:
//                                break;
//                        }
//                        scheduling.setDinnerStartTime(roster.getDinnerStartTime());
//                        scheduling.setDinnerEndTime(TimeUtil.offset(roster.getDinnerStartTime(), roster.getMainTime()));
//                        turn = false;
//                    } else if (!turn && roster != null) {
//                        Time time;
//                        switch (roster.getEatingType()) {
//                            case 0:
//                                // 轻餐开始时间=班次设定开始时间30分钟后
//                                time = TimeUtil.offset(roster.getLunchStartTime(), roster.getMainTime());
//                                scheduling.setLunchStartTime(time);
//                                // 轻餐结束时间=开始时间30分钟后
//                                scheduling.setLunchEndTime(TimeUtil.offset(time, roster.getLightTime()));
//                                break;
//                            case 1:
//                                // 就餐开始时间=班次设定开始时间30分钟后
//                                time = TimeUtil.offset(roster.getLunchStartTime(), roster.getMainTime());
//                                scheduling.setLunchStartTime(time);
//                                // 就餐结束时间=开始时间30分钟后
//                                scheduling.setLunchEndTime(TimeUtil.offset(time, roster.getMainTime()));
//                                break;
//                            case 2:
//                                break;
//                            default:
//                                break;
//                        }
//                        // 晚餐开始时间=班次设定开始时间30分钟后
//                        time = TimeUtil.offset(roster.getDinnerStartTime(), roster.getMainTime());
//                        scheduling.setDinnerStartTime(time);
//                        // 晚餐开始时间=晚餐开始时间30分钟后
//                        scheduling.setDinnerEndTime(TimeUtil.offset(time, roster.getMainTime()));
//                        turn = true;
//                    }
//                }else {
//                    Integer eatTimeId = worker.getEatTimeId();
//                    EatTime eatTime = eatTimeMapper.selectById(eatTimeId);
//                    switch (eatTime.getType()) {
//                        case 0:
//                            // 午餐就餐时间
//                            scheduling.setLunchStartTime(eatTime.getLunchStartTime());
//                            // 午餐就餐结束时间
//                            scheduling.setLunchEndTime(eatTime.getLunchEndTime());
//                            // 晚餐就餐时间
//                            scheduling.setDinnerStartTime(eatTime.getDinnerStartTime());
//                            // 晚餐结束时间
//                            scheduling.setDinnerEndTime(eatTime.getDinnerEndTime());
//                            break;
//                        case 1:
                            // 午餐就餐时间
//                            scheduling.setLunchStartTime(eatTime.getLunchStartTime());
//                            // 午餐就餐结束时间
//                            scheduling.setLunchEndTime(eatTime.getLunchEndTime());
//                            // 晚餐就餐时间
//                            scheduling.setDinnerStartTime(eatTime.getDinnerStartTime());
//                            // 晚餐结束时间
//                            scheduling.setDinnerEndTime(eatTime.getDinnerEndTime());
//                            break;
//                    }
                }

                if (scheduling.getDroster().equals("请假")&&scheduling.getType()== ScheduleTypeEnum.LEAVE){
                    scheduling.setStatus(WorkerApplyStatusEnum.WAIT);
                }else {
                    scheduling.setStatus(WorkerApplyStatusEnum.PASS);
                }
                schedulingList.add(scheduling);
            }
            schedulingService.updateBatchById(schedulingList);

        }
        schedulingExamine.updateById();
    }


}
